package edu.university.demo.system.entit;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.smallchill.core.toolbox.kit.CollectionKit;

@Entity
@Table(name = "pt_user")
public class User extends IdEntity {
	
	private String loginName;
	private String name;
	private String plainPassword;
	private String password;
	private String salt;
	private String roles;
	private Date registerDate;
	private Boolean isLock = false;
	private Integer passErrorCount = 0;
	private Date unlockTime;
	private List<Song> songList = Lists.newArrayList();

	public User() {
	}

	public User(Long id) {
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// 不持久化到数据库，也不显示在Restful接口的属性.
	@Transient
	@JsonIgnore
	public String getPlainPassword() {
		return plainPassword;
	}

	public void setPlainPassword(String plainPassword) {
		this.plainPassword = plainPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public String getRoles() {
		return roles;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}

	@Transient
	@JsonIgnore
	public List<String> getRoleList() {
		// 角色列表在数据库中实际以逗号分隔字符串存储，因此返回不能修改的List.
		return ImmutableList.copyOf(StringUtils.split(roles, ","));
	}

	// 设定JSON序列化时的日期格式
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}
	
	public Boolean getIsLock() {
		return isLock;
	}

	public void setIsLock(Boolean isLock) {
		this.isLock = isLock;
	}

	public Integer getPassErrorCount() {
		return passErrorCount;
	}

	public void setPassErrorCount(Integer passErrorCount) {
		this.passErrorCount = passErrorCount;
	}

	public Date getUnlockTime() {
		return unlockTime;
	}

	public void setUnlockTime(Date unlockTime) {
		this.unlockTime = unlockTime;
	}

	@ManyToMany(mappedBy="userList", fetch=FetchType.LAZY)
	public List<Song> getSongList() {
		return songList == null ? null : songList;
	}

	public void setSongList(List<Song> songList) {
		this.songList = songList;
	}
	
	public void recovery() {
		
		this.isLock = false;
		this.passErrorCount = 0;
	}
	
	@Transient
	public Long getSongNum() {
		
		if(CollectionKit.isEmpty(songList))
			return 0l;
		return (long) songList.size();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}